package com.aop.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JdkProxy implements InvocationHandler {
    private UserDao userDao;
    public Object createProxy(UserDao userDao) {
        this.userDao = userDao;//把目标对象传进来
       ClassLoader classLoader = JdkProxy.class.getClassLoader();//获取类加载器
       Class<?>[] interfaces = userDao.getClass().getInterfaces();//获取接口
        return Proxy.newProxyInstance(classLoader,interfaces, this);//创建代理对象

    }
  @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        MyAspect myAspect = new MyAspect();
        myAspect.check_Permissions();
        Object obj=method.invoke(userDao,args);//执行目标对象的方法
        myAspect.log();
        return obj;
    }

}
